package com.teamwork.module01.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module01.Class;
import com.teamwork.common.entity.module01.Paike;
import com.teamwork.common.entity.module01.Student;
import com.teamwork.common.entity.module01.dto.Kebiao;
import com.teamwork.common.result.Result;
import com.teamwork.module01.mapper.PaikeMapper;
import com.teamwork.module01.mapper.daomapper.KebiaoMapper;
import com.teamwork.module01.service.KebiaoService;
import com.teamwork.module01.service.PaikeService;
import com.teamwork.module01.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 *    课程表服务
 */
@Service
@Slf4j
public class PaikeServiceImpl extends ServiceImpl<PaikeMapper, Paike> implements PaikeService {
    @Resource
    private StudentService studentService;
    @Resource
    private KebiaoService kebiaoService;
    @Resource
    private PaikeMapper paikeMapper;
    @Override
    public Result getByStuId(Long id) {
       //先查询学生所在班级id
        LambdaQueryWrapper<Student> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Student::getId,id);
        Integer classId = studentService.getOne(wrapper).getClassId();
        //根据班级id再去查询排课
        LambdaQueryWrapper<Paike> paikeWrapper =new LambdaQueryWrapper<>();
        paikeWrapper.eq(Paike::getClassId,classId);
        Paike paike = this.getOne(paikeWrapper);
        //转为json
        Object json = JSONObject.toJSON(paike);

        return Result.success(json);
    }

    /**
     * 根据名称获取排课信息
     * @param name
     * @return
     */
    @Override
    public Result getByName(String name) {


        return null;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result getPage(Integer page, Integer size) {

        return   kebiaoService.page(page,size);

    }

    /**
     * 添加或者修改
     * @param paike
     * @return
     */
    @Override
    public Result InsertOrUpdate(Paike paike) {
        if(paike.getId()!=null){
            //修改
            boolean b = this.updateById(paike);
            //修改完排课后，应该把视图也修改了
           // boolean kb = kebiaoService.modify(paike);


            if(!b) return Result.failure("修改排课失败");
            log.info("id为:"+paike.getId()+"的排课信息已经修改");
            return Result.success("修改排课成功");
        }
        //添加
        boolean sb = this.save(paike);
       // boolean kb = kebiaoService.addOne(paike);
        if(!sb) return Result.failure("添加排课信息失败");


         log.info("添加了一条排课信息：{}",paike);
         return  Result.success("添加排课信息成功");


    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        return null;
    }

    /**
     * id数组批量删除
     * @param ids
     * @return
     */
    @Override
    public Result batchDelete(List<Integer> ids) {

        for (Integer id : ids) {
           // boolean b = deletePaikeAndKebiao(id);
            boolean b = this.removeById(id);
            if(!b) return Result.failure("删除失败");
            log.info("id为:"+id+"的排课被删除...");
        }


       return Result.success("删除成功");
    }

    //删除排课和课表信息
//    @Transactional
//    boolean deletePaikeAndKebiao(Integer id){
//        //先删除课表信息
//
//        boolean kb = kebiaoService.removeById(id);
//        //再删除排课信息
//        boolean pb = this.removeById(id);
//
//        return kb&&pb;
//
//
//    }

}
